package com.gaojinqi.sys.modular.rbac.service.impl;

import com.gaojinqi.base.common.service.imp.BaseServiceImpl;
import com.gaojinqi.sys.core.shiro.config.ShiroFilterConfiguration;
import com.gaojinqi.sys.modular.rbac.dao.RolePermissionRepository;
import com.gaojinqi.sys.modular.rbac.dao.RoleRepository;
import com.gaojinqi.sys.modular.rbac.dao.UserRoleRepository;
import com.gaojinqi.sys.modular.rbac.model.Role;
import com.gaojinqi.sys.modular.rbac.model.User;
import com.gaojinqi.sys.modular.rbac.model.UserRoleMapping;
import com.gaojinqi.sys.modular.rbac.service.RoleService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;

/**
 * @author gaojinqi
 * @version 1.0
 * @since 2020年04月21日
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role, String> implements RoleService {

    @Autowired
    private UserRoleRepository userRoleRepository;
    @Autowired
    private RolePermissionRepository rolePermissionRepository;
    @Autowired
    private RoleRepository roleRepository;
    @Autowired
    private ShiroFilterConfiguration shiroFilterConfiguration;

    @Override
    public List<String> findRoleIdsByUserId(String userId) {
        return userRoleRepository.findRoleIdsByUserId(userId);
    }

    @Override
    public List<Role> findRolesByUserId(String userId) {
        return userRoleRepository.findRolesByUserId(userId);
    }

    @Override
    public List<Role> findRolesByUserName(String userName) {
        return userRoleRepository.findRolesByUserName(userName);
    }

    @Override
    public Role findByRole(String role) {
        return roleRepository.findByRole(role);
    }

    @Override
    public void assignRolesToUserId(String userId, Collection<String> roleIds) {
        // 删除指定用户所关联的角色
        userRoleRepository.deleteByUserId(userId);
        // 重新给指定用户分配角色
        List<UserRoleMapping> mappingList = Lists.newArrayList();
        User user = new User();
        user.setUserId(userId);
        for (String roleId : roleIds) {
            UserRoleMapping mapping = new UserRoleMapping();
            Role role = new Role();
            role.setRoleId(roleId);
            mapping.setUser(user);
            mapping.setRole(role);
            mappingList.add(mapping);
        }
        if (!CollectionUtils.isEmpty(mappingList)) {
            userRoleRepository.saveSelective(mappingList);
            // 刷新规则
            shiroFilterConfiguration.reloadFilterChain();
        }
    }

    @Override
    @Transactional
    public void deleteById(String roleId) {
        // 删除角色权限关系
        rolePermissionRepository.deleteByRoleId(roleId);
        // 删除角色用户关系
        userRoleRepository.deleteByRoleId(roleId);
        // 删除角色
        super.deleteById(roleId);
        // 刷新权限
        shiroFilterConfiguration.reloadFilterChain();
    }

    @Override
    @Transactional
    public void deleteByIds(List<String> roleIds) {
        // 删除角色权限关系
        rolePermissionRepository.deleteByRoleIds(roleIds);
        // 删除角色用户关系
        userRoleRepository.deleteByRoleIds(roleIds);
        // 删除角色
        super.deleteByIds(roleIds);
        // 刷新权限
        shiroFilterConfiguration.reloadFilterChain();
    }


}
